/*
 * Test module.
 * $Rev: 166 $
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "file_db.h"
#include "utils.h"
#include "md5lib/md5_utils.h"

#define DIR "./downloads"
#define EMPTY_MD5 "d41d8cd98f00b204e9800998ecf8427e"

#define DUMMY_FILE "some_dummy_file.txt"
#define DUMMY_MD5 "553adeededbdcbf338a9b027f6c9a5f7"

int main(void) {
    file_db *db = NULL;
    file *file = NULL;
    unsigned int res_len;
    unsigned int i = 0;
    md5_t *complete_files = NULL, *fragments_in_disk = NULL;
    off_t s = 0;

    if (!path_is_directory(DIR)) {
            assert(mkdir(DIR, S_IRWXU) == 0);
        }
    assert(path_is_directory(DIR));
    db = file_db_create(DIR);
    assert(db != NULL);

    /*complete_files = file_db_get_complete_files(db, &res_len);
    printf("*** test.c: main: file database returned %i complete files!\n", res_len);
    for (i = 0; i < res_len; i++) printf("file %i-th: %s\n", i, complete_files[i]);
    for (i = 0; i < res_len; i++) free(complete_files[i]);
    free(complete_files);*/

    printf("*** test.c: main: creating a file thru file database...\n");
    file = file_db_create_file(db, EMPTY_MD5, "empty_file", 0);
    printf("*** test.c: main: first file returned from db: %p\n", file);
    assert(file_is_partial(file));
    assert(!file_has_index_info(file));
    assert(file_size(file) == 0);
    printf("*** test.c: main: file fragment count %i\n", file_fragment_count(file));
    printf("*** test.c: main: file name %s\n", file_name(file));
    printf("*** test.c: main: file md5 %s\n", file_md5(file));

    assert(file_db_mark_as_complete(db, EMPTY_MD5));

    printf("*** test.c: main: creating another, non-empty file thru file database...\n");
    system("cp hash_table/hashtable.c " DUMMY_FILE);
    s = path_size("./" DUMMY_FILE);
    printf("*** test.c: main: before creating file from db: size %li, md5: %s\n", s, DUMMY_MD5);
    file = file_db_create_file(db, DUMMY_MD5, DUMMY_FILE, s);
    printf("*** test.c: main: second file returned from db: %p\n", file);
    assert(file_is_partial(file));
    assert(!file_has_index_info(file));
    assert(file_size(file) == s);
    printf("*** test.c: main: file fragment count %i\n", file_fragment_count(file));
    printf("*** test.c: main: file name %s\n", file_name(file));
    printf("*** test.c: main: file md5 %s\n", file_md5(file));

    system("cp hash_table/hashtable.c " DIR "/partial/" DUMMY_MD5 "." DUMMY_FILE);
    assert(file_db_mark_as_complete(db, DUMMY_MD5));

    complete_files = file_db_get_complete_files(db, &res_len);
    printf("*** test.c: main: file database returned %i complete files!\n", res_len);
    for (i = 0; i < res_len; i++) printf("file %i-th: %s\n", i, complete_files[i]);
    for (i = 0; i < res_len; i++) free(complete_files[i]);
    free(complete_files);

    /* testing file_db_get_file for EMPTY_FILE */
    file = file_db_get_file(db, EMPTY_MD5);

    /* testing file_fragments_in_disk for EMPTY_FILE  */
    fragments_in_disk = file_fragments_in_disk(file, &res_len);
    printf("*** test.c: main: file returned %i fragments in disk!\n", res_len);
    for (i = 0; i < res_len; i++) printf("fragment %i-th: %s\n", i, fragments_in_disk[i]);
    for (i = 0; i < res_len; i++) free(fragments_in_disk[i]);
    free(fragments_in_disk);

    /* testing file_db_get_file for DUMMY_FILE */
    file = file_db_get_file(db, DUMMY_MD5);

    /* testing file_fragments_in_disk for DUMMY_FILE */
    fragments_in_disk = file_fragments_in_disk(file, &res_len);
    printf("*** test.c: main: file returned %i fragments in disk!\n", res_len);
    for (i = 0; i < res_len; i++) printf("fragment %i-th: %s\n", i, fragments_in_disk[i]);
    for (i = 0; i < res_len; i++) free(fragments_in_disk[i]);
    free(fragments_in_disk);

    file_db_destroy(db);

    system("rm -f " DUMMY_FILE);
    system("rm -rf " DIR);
    return 0;
}
